package com.weiyuwqj.admin.controller.admin.account;

import com.weiyuwqj.admin.controller.base.BaseController;
import com.weiyuwqj.admin.controller.admin.log.annotation.LogAnnotation;
import com.weiyuwqj.api.constant.Global;
import com.weiyuwqj.api.exception.HfException;
import com.weiyuwqj.api.service.*;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.model.BusAccountCash;
import com.weiyuwqj.api.model.convert.AccountCashAddtimeConvert;
import com.weiyuwqj.api.model.convert.AccountCashStatusConvert;
import com.weiyuwqj.api.model.convert.AccountCashTypeConvert;
import com.weiyuwqj.api.model.convert.ModelConvert;
import com.weiyuwqj.api.model.vo.BusAccountCashVo;
import com.weiyuwqj.api.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;


/**
 * The function of saving, modifying and deleting the site module
 * <p>
 * Copyright 2015-2019 Yunfucloud technology co., Ltd.
 * Created by hanling on 15/10/24.
 */
@Controller
@RequestMapping("/admin/accountcash")
public class AccountCashController extends BaseController {

    @Autowired
    private BusAccountCashService busAccountCashService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private BusAccountService accountService;

    @Autowired
    private BasUserService basUserService;

    /**
     * 根据订单号来修改订单的状态
     *
     * @param orderNo   订单号
     * @param oldStatus 旧status
     * @param newStatus 新status
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @LogAnnotation(module = "账户管理/提现管理", description = "修改提现订单状态")
    public Object updateStatus(String orderNo, int oldStatus, int newStatus) {
        String addIp = IpUtils.get();
        busAccountCashService.updateBusAccountCashStatus(orderNo, oldStatus, newStatus, addIp);
        return Message.OK;
    }

    /**
     * 列出所有提现记录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(
            @ModelAttribute Paginator p,
            @RequestParam(defaultValue = "") String userId,
            @RequestParam(defaultValue = "") String commonCond,
            @RequestParam(defaultValue = "0") int type,
            @RequestParam(defaultValue = "-1") int status, HttpServletRequest request) {
        String beginDateTime = request.getParameter("beginDateTime");
        String endDateTime = request.getParameter("endDateTime");
        String orderName = request.getParameter("orderName");
        String orderWay = request.getParameter("orderWay");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("commonCond", commonCond);
        map.put("type", type);
        map.put("status", status);
        if (StringUtils.isNotEmpty(beginDateTime)) {
            map.put("beginDateTime", beginDateTime);
        }
        if (StringUtils.isNotEmpty(endDateTime)) {
            map.put("endDateTime", endDateTime);
        }
        if (StringUtils.isNotEmpty(orderName)) {
            map.put("orderName", orderName);
        }
        if (StringUtils.isNotEmpty(orderName)) {
            map.put("orderWay", orderWay);
        }

        return busAccountCashService.getList(p, map);
    }

    /**
     * 用户提现导出
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/excel/accountCash")
    public Object accountCashExcel(HttpServletRequest request, String userId, String type, String status, String commonCond, String beginDateTime, String endDateTime) {

        String[] cArray = {"ID", "用户名", "真实姓名", "流水号", "所属银行", "银行卡号", "提现现金", "到账金额", "提现费用", "类型", "状态", "添加时间"};
        String[] eArray = {"Id", "Username", "Realname", "OrderNo", "BankName", "CardNo", "CashAmount", "ActualAmount", "FeeAmount", "CashType", "CashStatus", "Addtime"};
        ModelConvert[] converts = {null, null, null, null, null, null, null, null, null, new AccountCashTypeConvert(), new AccountCashStatusConvert(), new AccountCashAddtimeConvert()};

        List<String> cList = Arrays.asList(cArray);
        List<String> eList = Arrays.asList(eArray);
        List<ModelConvert> convertList = Arrays.asList(converts);
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("type", type);
        map.put("commonCond", commonCond);
        map.put("status", status);
        map.put("beginDateTime", beginDateTime);
        map.put("endDateTime", endDateTime);

        List<BusAccountCashVo> contentList = busAccountCashService.getList(map);

        String serverDir = request.getServletContext().getRealPath("/") + "admin/export/";
        String excelFilePath = ExcelUtils.export("提现管理", cList, eList, convertList, contentList, serverDir);
        HashMap ret = Message.OK;
        ret.put("file", "export/" + excelFilePath);
        return ret;
    }


    /**
     * 列出订单号记录记录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getSingle", method = RequestMethod.POST)
    public Object getSingle(
            @RequestParam(defaultValue = "") String orderNo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderNo", orderNo);
        return busAccountCashService.getSingle(map);
    }


    @ResponseBody
    @RequestMapping(value = "/getAcutalAmount", method = RequestMethod.POST)
    public Object getAcutalAmount(Integer userId) {
        return busAccountCashService.getTodayWithdrawTotal(userId);
    }


    /**
     * 查询当月提现次数
     */
    @ResponseBody
    @RequestMapping(value = "/getMonthlyWithdrawTotal", method = RequestMethod.POST)
    public Object getMonthlyWithdrawTotal(Integer userId) {
        Date date = new Date();
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("startTime", DateUtils.getMonthStartTime(date));
        map.put("endTime", DateUtils.getNextMonthStartTime(date));
        return busAccountCashService.getMonthlyWithdrawTotal(map);
    }


    /**
     * 根据提现来源统计提现总额
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statCashAmount", method = RequestMethod.POST)
    public Object statCashAmount(@RequestParam(defaultValue = "") String userId,
                                 @RequestParam(defaultValue = "") String commonCond,
                                 @RequestParam(defaultValue = "0") int type,
                                 @RequestParam(defaultValue = "-1") int status, HttpServletRequest request) {
        String beginDateTime = request.getParameter("beginDateTime");
        String endDateTime = request.getParameter("endDateTime");
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("userId", userId);
        queryMap.put("commonCond", commonCond);
        queryMap.put("type", type);
        queryMap.put("status", status);
        if (StringUtils.isNotEmpty(beginDateTime)) {
            queryMap.put("beginDateTime", beginDateTime);
        }
        if (StringUtils.isNotEmpty(endDateTime)) {
            queryMap.put("endDateTime", endDateTime);
        }
        Map map = new HashMap();
        Map ret = new HashMap();
        BigDecimal[] val = {BigDecimal.ZERO, BigDecimal.ZERO};
        List<Map> list = busAccountCashService.statCashAmount(queryMap);
        for (int i = 0; i < list.size(); i++) {
            map = list.get(i);
            BigDecimal num = (BigDecimal) map.get("cashAmount");
            int no = (int) map.get("status");
            val[no - 1] = num;
        }
        ret.put("cashAccount", val[0].add(val[1]));
        ret.put("cashAuditAccount", val[1]);
        return ret;
    }

    /**
     * 提现
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/cash")
    @LogAnnotation(module = "账户管理/提现管理", description = "提现")
    public Object cash(BigDecimal amount,String resource) {
        Integer userId = this.getSessionUserId();
        return busAccountCashService.doCash(amount, userId, resource);
    }

    /**
     * 列出所有提现记录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listByGuaranteeId", method = RequestMethod.POST)
    public Object list(
            @ModelAttribute Paginator p,
            @RequestParam(defaultValue = "") String commonCond,
            @RequestParam(defaultValue = "0") int type,
            @RequestParam(defaultValue = "-1") int status, HttpServletRequest request) {
        Integer userId = this.getSessionUserId();
        String beginDateTime = request.getParameter("beginDateTime");
        String endDateTime = request.getParameter("endDateTime");
        String orderName = request.getParameter("orderName");
        String orderWay = request.getParameter("orderWay");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("commonCond", commonCond);
        map.put("type", type);
        map.put("status", status);
        if (StringUtils.isNotEmpty(beginDateTime)) {
            map.put("beginDateTime", beginDateTime);
        }
        if (StringUtils.isNotEmpty(endDateTime)) {
            map.put("endDateTime", endDateTime);
        }
        if (StringUtils.isNotEmpty(orderName)) {
            map.put("orderName", orderName);
        }
        if (StringUtils.isNotEmpty(orderName)) {
            map.put("orderWay", orderWay);
        }

        return busAccountCashService.getList(p, map);
    }

//    /**
//     * 平台提现
//     *
//     * @param amount
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/platformWithdraw", method = RequestMethod.POST)
//    public Object platformWithdraw(String amount, String resource) {
//        //请求提现, 添加记录
//        BusAccountCash cash = busAccountCashService.doCash(new BigDecimal(amount),
//                Integer.parseInt(Global.getOwnChildAccountId()),resource);
//        try {
//            //请求恒丰接口
//            busAccountCashService.platformWithdraw(amount, cash.getOrderNo());
//        } catch (Exception e) {
//            busAccountCashService.updateBusAccountCashStatus(cash.getOrderNo(), 2, 0, IpUtils.get());
//            busAccountCashService.updateRemarkByOrderNo(cash.getOrderNo(), cash.getRemark());
//            throw new HfException("HfException", "恒丰错误");
//        }
//        return Message.OK;
//    }


}